package com.yunxin.core.infra.persistence.repository.page;

import com.yunxin.core.domain.model.page.PagePopupUserRecord;
import com.yunxin.core.domain.model.page.PagePopupUserRecordRepository;
import com.yunxin.core.infra.persistence.converter.PageConverter;
import com.yunxin.core.infra.persistence.entity.page.PagePopupUserRecordDO;
import com.yunxin.core.infra.persistence.mapper.page.PagePopupUserRecordExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class PagePopupUserRecordRepositoryImpl implements PagePopupUserRecordRepository {

    private final PagePopupUserRecordExtendMapper pagePopupUserRecordExtendMapper;

    private final PageConverter pageConverter;

    @Override
    public boolean savePagePopupUserRecord(PagePopupUserRecord pagePopupUserRecord) {
        PagePopupUserRecordDO oldPagePopupUserRecordDO = pagePopupUserRecordExtendMapper.selectByUserCodeAndElementCode(pagePopupUserRecord.getUserCode(), pagePopupUserRecord.getElementCode());
        if (Objects.nonNull(oldPagePopupUserRecordDO)) {
            return false;
        }
        PagePopupUserRecordDO pagePopupUserRecordDO = pageConverter.fromPagePopupUserRecord(pagePopupUserRecord);
        int count = pagePopupUserRecordExtendMapper.insertSelective(pagePopupUserRecordDO);
        return count > 0;
    }
}
